package com.xyh.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyh.commonutils.R;
import com.xyh.eduservice.entity.EduTeacher;
import com.xyh.eduservice.entity.vo.TeacherQuery;
import com.xyh.eduservice.service.EduTeacherService;
import com.xyh.servicebase.exceptionHandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-03
 */
@Api(value = "讲师管理")  //所有 Api 啥的接口可有可无，为了让 Swagger 页面更好看
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin  //解决跨域
public class EduTeacherController {

    @Resource
    private EduTeacherService eduTeacherService;

    //查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
//        try {
//            int i =10/0;
//        } catch (Exception e) {
//            throw new GuliException(20001,"捕获到GuliException异常");
//        }
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable("id") String id) {
        boolean b = eduTeacherService.removeById(id);
        return b ? R.ok() : R.error();
    }

    //分页查询讲师的方法

    /**
     * current : 当前页
     * limit : 页数
     */
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") Long current, @PathVariable("limit") Long limit) {
        //创建 page 对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        eduTeacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        //第一种方式:
//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);
        //第二种方式:
        return R.ok().data("total", total).data("rows", records);
        //第三种方式:
//        return R.ok().data("result",pageTeacher);
    }

    //条件查询带分页

    /**
     * @RequestBody(required = false) : 使用 json 传递数据，把 json 数据封装到对应对象里面 (required = false : 这个值可以没有)(要用 post 请求)
     * @ResponseBody : 返回数据 , 返回 json 数据
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") Long current, @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建 page 对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询(有的条件没有就不需要写入sql)
        //判断条件值是否为空，如果不为空则拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            //构造条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现实现条件查询分页
        eduTeacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
//        return R.ok().data("result",pageTeacher);
        return R.ok().data("total", total).data("rows", records);
    }

    //添加讲师接口方法
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? R.ok() : R.error();
    }

    //根据讲师id进行查询
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    //讲师修改
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = eduTeacherService.updateById(eduTeacher);
        return b ? R.ok() : R.error();
    }


}

